import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import router from "./router";
import { createPinia } from "pinia";
import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
  RouteRecordRaw,
} from "vue-router";

const pinia = createPinia();

declare global {
  interface Window {
    // 是否存在无界
    __POWERED_BY_WUJIE__?: boolean;
    // 子应用mount函数
    __WUJIE_MOUNT: () => void;
    // 子应用unmount函数
    __WUJIE_UNMOUNT: () => void;
    // 子应用无界实例
    __WUJIE: { mount: () => void };
  }
}

let instance: any = createApp(App);
if (window.__POWERED_BY_WUJIE__) {
  // console.log("window.__POWERED_BY_WUJIE__", window.__POWERED_BY_WUJIE__);
  console.log("window.parent");
  window.__WUJIE_MOUNT = () => {
    console.log("__WUJIE_MOUNT");
    instance = createApp(App);
    instance.use(router).use(pinia);
    console.log("instance", instance, App);
    instance.mount("#root");
  };
  window.__WUJIE_UNMOUNT = () => {
    console.log("__WUJIE_UNMOUNT");
    instance.unmount();
  };
  window.__WUJIE.mount();
} else {
  instance.use(router).use(pinia).mount("#root");
}

// let instance;
// if (window.__POWERED_BY_WUJIE__) {
//   window.__WUJIE_MOUNT = () => {
//     instance = createApp(App);
//     instance.use(router);
//     instance.mount("#app");
//   };
//   window.__WUJIE_UNMOUNT = () => {
//     instance.unmount();
//   };
// } else {
//   createApp(App).use(router).mount("#app");
// }

export default instance;
